/*
 * @Author: Joel
 * @Date: 2025-04-10 16:52:07
 * @LastEditors: Joel
 * @LastEditTime: 2025-04-12 09:32:47
 * @FilePath: /smart-cupping2/usr/board_driver/control.c
 * @Description: 
 * Copyright (C) 2025 YiJiaLink, All Rights Reserved. 
 * 2025-04-10 16:52:07
 */

#include"control.h"

static void control_gpio_init(void);
static void run_control(enum CTRL_OUTPUT ctrl,uint8_t flag);
static void stop_control(enum CTRL_OUTPUT ctrl);
static struct ctrl_func  ctrl;

/**
 * @func: 
 * @description: 初始化rled
 * @param {rled_func} rled_func
 * @return {*}
 * @example: 
 */
struct ctrl_func *ctrl_driver_init(void)
{
    struct ctrl_func *c_func = &ctrl;
    c_func->device_init = control_gpio_init;
    c_func->ctrl_run = run_control;
    c_func->ctrl_stop = stop_control;

    return c_func;
}
/**
 * @func: 
 * @description: 初始化GPIO口
 * @return {*}
 * @example: 
 */
static void control_gpio_init(void)
{
    /* configure GPIO port */
	PORT_Init(PWR_CTRL_GPIO_PORT,PWR_CTRL_PIN,OUTPUT);
    PORT_Init(VALVE_CTRL_GPIO_PORT,VALVE_CTRL_PIN,OUTPUT);
    PORT_Init(FSTCH_CTRL_GPIO_PORT,FSTCH_CTRL_PIN,OUTPUT);
    PORT_Init(EMS_CTRL_GPIO_PORT,EMS_CTRL_PIN,OUTPUT);

    PORT_Init(OLED_RES_GPIO_PORT,OLED_RES_PIN,OUTPUT);
    PORT_Init(OLED_DC_GPIO_PORT,OLED_DC_PIN,OUTPUT);
    PORT_Init(OLED_CS_GPIO_PORT,OLED_CS_PIN,OUTPUT);
    PORT_Init(OLED_SCLK_GPIO_PORT,OLED_SCLK_PIN,OUTPUT);
    PORT_Init(OLED_SDIN_GPIO_PORT,OLED_SDIN_PIN,OUTPUT);

    PORT_Init(IR_CTRL_GPIO_PORT,IR_CTRL_PIN,OUTPUT);

    return;
}
/**
 * @func: 
 * @description: 输出电平控制
 * @param {enum CTRL_OUTPUT} ctrl
 * @param {uint8_t} flag
 * @return {*}
 * @example: 
 */
static void run_control(enum CTRL_OUTPUT ctrl,uint8_t flag)
{
    uint8_t gpiox, pin;
    switch (ctrl)
    {
    case PWR_CTRL:
        /* code */
        gpiox = PWR_CTRL_GPIO_PORT;
        pin = PWR_CTRL_PIN;
        break;
    case VALVE_CTRL:
        /* code */
        gpiox = VALVE_CTRL_GPIO_PORT;
        pin = VALVE_CTRL_PIN;
        break;
    case FSTCH_CTRL:
        /* code */
        gpiox = FSTCH_CTRL_GPIO_PORT;
        pin = FSTCH_CTRL_PIN;
        break;
    case EMS_CTRL:
        /* code */
        gpiox = EMS_CTRL_GPIO_PORT;
        pin = EMS_CTRL_PIN;
        break;
    case OLED_RES:
        /* code */
        gpiox = OLED_RES_GPIO_PORT;
        pin = OLED_RES_PIN;
        break;
    case OLED_DC:
        /* code */
        gpiox = OLED_DC_GPIO_PORT;
        pin = OLED_DC_PIN;
        break;
    case OLED_CS:
        /* code */
        gpiox = OLED_CS_GPIO_PORT;
        pin = OLED_CS_PIN;
        break;
    case IR_CTRL:
        /* code */
        gpiox = IR_CTRL_GPIO_PORT;
        pin = IR_CTRL_PIN;
        break;
    default:
        break;
    }

    if(0 != flag){
        PORT_SetBit(gpiox, pin);
    }else{
        PORT_ClrBit(gpiox, pin);
    }
    return;
}
/**
 * @func: 
 * @description: 关闭控制
 * @return {*}
 * @example: 
 */
static void stop_control(enum CTRL_OUTPUT ctrl)
{
    uint8_t gpiox, pin;
    switch (ctrl)
    {
    case PWR_CTRL:
        /* code */
        gpiox = PWR_CTRL_GPIO_PORT;
        pin = PWR_CTRL_PIN;
        break;
    case VALVE_CTRL:
        /* code */
        gpiox = VALVE_CTRL_GPIO_PORT;
        pin = VALVE_CTRL_PIN;
        break;
    case FSTCH_CTRL:
        /* code */
        gpiox = FSTCH_CTRL_GPIO_PORT;
        pin = FSTCH_CTRL_PIN;
        break;
    case EMS_CTRL:
        /* code */
        gpiox = EMS_CTRL_GPIO_PORT;
        pin = EMS_CTRL_PIN;
        break;
    case OLED_RES:
        /* code */
        gpiox = OLED_RES_GPIO_PORT;
        pin = OLED_RES_PIN;
        break;
    case OLED_DC:
        /* code */
        gpiox = OLED_DC_GPIO_PORT;
        pin = OLED_DC_PIN;
        break;
    case OLED_CS:
        /* code */
        gpiox = OLED_CS_GPIO_PORT;
        pin = OLED_CS_PIN;
        break;
    case IR_CTRL:
        /* code */
        gpiox = IR_CTRL_GPIO_PORT;
        pin = IR_CTRL_PIN;
        break;
    default:
        break;
    }
    PORT_ClrBit(gpiox, pin);
    return;
}
